package nl.saxion.act.servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import nl.saxion.act.dao.AuctionDAO;
import nl.saxion.act.dao.CatagoryDAO;
import nl.saxion.act.db.DatabaseManager;

/**
 * Servlet implementation class VeilingServlet
 */
public class VeilingServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private DatabaseManager dbMan;
	private ServletContext context;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public VeilingServlet() {
        super();
    }
    
    public void init() throws ServletException {
    	context = this.getServletContext();
    	dbMan = (DatabaseManager) context.getAttribute("dbMan");
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		AuctionDAO auction = new AuctionDAO(dbMan);
		if(request.getParameter("Action") != null){
			if(request.getParameter("Action").equalsIgnoreCase("Catagory")){
				request.setAttribute("veilingen", auction.getAuctionsCatagory(request.getParameter("catagory")) ) ;
			}else{
				request.setAttribute("veilingen", auction.getFirstFewAuctions() ) ;
			}
		}
		CatagoryDAO catagory = new CatagoryDAO(dbMan);
		request.setAttribute("catagories", catagory.getAllCatagory() );
		
		RequestDispatcher view = context.getRequestDispatcher("/index.jsp");
		view.forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
